﻿namespace TriangleGames.TrainResEditor
{
    using System;
    using UnityEditor;
    using UnityEngine;

    public class WelcomeWindow : EditorWindow
    {
        private bool showAtStartup;

        private static void Link(Texture texture, string heading, string body, string url)
        {
            GUILayout.Space(10f);
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            GUILayout.Space(64f);
            GUILayout.Box(texture, GUIStyle.none, new GUILayoutOption[] { GUILayout.MaxWidth(48f) });
            GUILayout.Space(10f);
            GUILayout.BeginVertical(new GUILayoutOption[0]);
            GUILayout.Label(heading, EditorStyles.boldLabel, new GUILayoutOption[0]);
            GUILayout.Label(body, new GUILayoutOption[0]);
            GUILayout.EndVertical();
            GUILayout.EndHorizontal();
            Rect lastRect = GUILayoutUtility.GetLastRect();
            EditorGUIUtility.AddCursorRect(lastRect, MouseCursor.Link);
            if ((Event.current.type == EventType.MouseDown) && lastRect.Contains(Event.current.mousePosition))
            {
                Application.OpenURL(url);
            }
            GUILayout.Space(10f);
        }

        public void OnEnable()
        {
            base.title = "欢迎";
            base.minSize = new Vector2(500f, 400f);
            base.maxSize = new Vector2(500f, 401f);
            this.showAtStartup = EditorPrefs.GetBool("TrainRes.ShowWelcomeScreen", true);
        }

        public void OnGUI()
        {
            if (!FsmEditorStyles.IsInitialized())
            {
                FsmEditorStyles.Init();
            }
            FsmEditorGUILayout.TrainResHeader(this);
            GUILayout.BeginVertical(new GUILayoutOption[0]);
            GUILayout.FlexibleSpace();
            Link(FsmEditorStyles.VideoIcon, "视频教程", "查看视频教程.", "http://www.triangle-games.com/tutorials.html");
            Link(FsmEditorStyles.DocsIcon, "在线文档", "查看在线文档.\n", "http://www.triangle-games.com/tutorials.html");
            Link(FsmEditorStyles.ForumIcon, "论坛", "加入论坛!\n", "http://www.triangle-games.com/forum/index.php");
            GUILayout.FlexibleSpace();
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            GUILayout.FlexibleSpace();
            bool flag = GUILayout.Toggle(this.showAtStartup, "启动时显示", new GUILayoutOption[0]);
            if (flag != this.showAtStartup)
            {
                this.showAtStartup = flag;
                EditorPrefs.SetBool("TrainRes.ShowWelcomeScreen", this.showAtStartup);
            }
            GUILayout.Space(10f);
            GUILayout.EndHorizontal();
            GUILayout.EndVertical();
        }
    }
}

